﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HitsInMin
{
    public partial class Form1 : Form
    {
        List<DateTime> hit = new List<DateTime>();
        
        public Form1()
        {
            InitializeComponent();
            lblAll.Text = "нет";
            lbl10.Text = "нет";
            lbl50.Text = "нет";
            lbl100.Text = "нет";
            lblLastHits.Text = "";
        }

        private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                hit.Clear();
                hit.Add(DateTime.Now);
            }

            if (e.KeyCode == Keys.Space && hit.Count > 0)
            {
                hit.Add(DateTime.Now);
            }

            if (hit.Count < 2)
            {
                lblAll.Text = "0";
                lbl10.Text = "нет";
                lbl50.Text = "нет";
                lbl100.Text = "нет";
                lblLastHits.Text = "";
            }
            else
            {
                double sumAll = 0;
                double sum20 = 0;
                double sum50 = 0;
                double sum100 = 0;

                String lastHits = "";
                for (int i = hit.Count - 1; i > 0; i--)
                {
                    double n = (hit[i] - hit[i - 1]).TotalMilliseconds;
                    sumAll += n;
                    if (i >= (hit.Count - 20))
                    {
                        sum20 += n;
                        lastHits += (60000 / n).ToString("F0") + " ";
                    }
                    if (i >= (hit.Count - 50)) sum50 += n;
                    if (i >= (hit.Count - 100)) sum100 += n;
                }

                double cnt = (double)(hit.Count - 1);
                lblAll.Text = Math.Round(60000 / (sumAll / cnt), 1).ToString();
                if (cnt >= 20) lbl10.Text = Math.Round(60000 / (sum20 / 20.0), 1).ToString();
                if (cnt >= 50) lbl50.Text = Math.Round(60000 / (sum50 / 50.0), 1).ToString();
                if (cnt >= 100) lbl100.Text = Math.Round(60000 / (sum100 / 100.0), 1).ToString();
                lblLastHits.Text = lastHits;
            }

            lblCnt.Text = hit.Count.ToString();
        }
    }
}
